//恒达货代发票信息的crud

import axios from "axios";
import {ElMessage} from "element-plus";
import {useStore} from "vuex";
import moment from "moment";


//添加恒达货代发票信息数据
export async function saveHengdaInvoice(data){
    try {
        const response = await axios.post('http://localhost:8888/hengda_invoice/save',data)
        if (response.data.code === '00000'){
            ElMessage.success('添加成功!')
        }else {
            ElMessage.error(response.data.message)
        }
    }catch (e) {
        ElMessage.error(e.message)
    }
}

//修改恒达货代发票信息数据
export async function modifyOrInsertHengdaInvoice(data){
    try {
        const response = await axios.post('http://localhost:8888/hengda_invoice/modify_or_insert',data)
        if (response.data.code === '00000'){
            ElMessage.success('修改成功!')
        }else {
            ElMessage.error(response.data.message)
        }
    }catch (e) {
        ElMessage.error(e.message)
    }
}

//修改恒达货代发票信息数据
export async function removeHengdaInvoice(data){
    try {
        const response = await axios.post('http://localhost:8888/hengda_invoice/remove_by_fba',data)
        if (response.data.code === '00000'){
            ElMessage.success(`删除${data.forwarderId}成功!`)
        }else {
            ElMessage.error(response.data.message)
        }
    }catch (e) {
        ElMessage.error(e.message)
    }
}

//根据fba获取恒达货代发票
export async function getHengdaInvoiceByFba(data){
    try {
        const response = await axios.post('http://localhost:8888/hengda_invoice/get_by_fba',data)
        if (response.data.code === '00000'){
            return response.data.data || []
        }else {
            ElMessage.error(response.data.message)
        }
    }catch (error) {
        console.log(error.message)
    }
}

//根据员工id获取恒达货代发票
export async function getHengdaInvoiceByStaffId(){
    let store = useStore();
    let  staffInfo = store.state.staffInfo
    try {
        const response = await axios.post('http://localhost:8888/hengda_invoice/get_by_staff_id',{staffId:staffInfo.staffId})
        if (response.data.code === '00000'){
            return response.data.data || []
        }else {
            ElMessage.error(response.data.message)
        }
    }catch (error) {
        console.log(error.message)
    }
}

//导入数据函数
export async function submitUpload(file){
    if (!file) return;
    const formData = new FormData();
    formData.append('file', file)
    try {
        const response = await axios.post('http://localhost:8888/hengda_invoice/batch_upload', formData, {
            headers: { 'Content-Type': 'multipart/form-data' }
        });
        if (response.data.code === '00000'){
            ElMessage.success(`导入成功`);
        }else {
            ElMessage.error(`导入失败: ${response.data.message}`);
            console.error('导入失败:', response.data);
        }

    } catch (error) {
        ElMessage.error(`导入失败: ${error.message}`);
        console.error('导入失败:', error);
    } finally {
        file = null;
    }
}

//下载模板函数
export async function downloadTemplate(staffInfo){
    try {
        const response = await axios.get(`http://localhost:8888/hengda_invoice/download_template?staffId=${staffInfo.staffId}`,{
            responseType: 'blob'
        });

        const url = window.URL.createObjectURL(new Blob([response.data]));
        const link = document.createElement('a');
        link.href = url;
        link.setAttribute('download', '恒达货代发票信息.xlsx');
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
    } catch (error) {
        console.error('下载失败:', error);
        ElMessage.error('模板下载失败');
    }
}

//请求生成发票信息
export async function generateInvoice(hengdaInvoiceList){
    if (!hengdaInvoiceList || hengdaInvoiceList.length == 0){
        ElMessage.error('请选择需要生成唛头的数据');
        return
    }
    try {
        const response = await axios.post('http://localhost:8888/hengda_invoice/generate_invoice',hengdaInvoiceList,{responseType: 'blob'});

        if (response.status === 200){
            const zipFileName = moment().format("YYYYMMDD")+"-唛头.zip"
            // 创建下载链接
            const url = window.URL.createObjectURL(new Blob([response.data]));
            const link = document.createElement('a');
            link.href = url;
            link.setAttribute('download', zipFileName); // 设置下载文件名
            document.body.appendChild(link);
            link.click();
            // 清理
            link.remove();
            window.URL.revokeObjectURL(url);
        }else {
            ElMessage.error("货代发票生成失败")
        }
    }catch (error) {
        console.log(error)
    }
}